const { ListNode, List, List2Array } = require('./common')
var reverseBetween = function (head, left, right) {
  let dummyHead = new ListNode(0)
  dummyHead.next = head
  if (head) {
    let p = dummyHead
    for (let i = 0; i < left - 1; i++) {
      p = p.next
    }
    let preNode = p
    let start = p.next
    let pre = p
    let cur = start
    for (let i = left; i <= right; i++) {
      let tmp = cur.next
      cur.next = pre
      pre = cur
      cur = tmp
    }

    preNode.next = pre
    start.next = cur
  }
  return dummyHead.next
}









function reverseBetween(head, left, right) {
  let dummyHead = new ListNode(0)
  let p = dummyHead
  p.next = head
  if (head) {

    for (let i = 1; i < left; i++) {
      p = p.next
    }
    let preNode = p
    let tail = p.next
    let pre = p
    let cur = p.next
    for (let i = left; i <= right; i++) {
      let tmp = cur.next
      cur.next = pre
      pre = cur
      cur = tmp
    }
    preNode.next = pre
    tail.next = cur
  }
  return dummyHead.next
}

console.log(List2Array(reverseBetween(List([3, 5]), 1, 2)))